package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoSurroundCompany;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 周围企业表操作类
 * 
 * @author Administrator
 *
 */
@Alias(value = "surroundCompany")
public class SurroundCompanyUI extends UIFacade {
	/**
	 * 增加或更改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		String surroundCompanyList = $V("SurroundCompanyList");
		if (StringUtil.isNotEmpty(surroundCompanyList)) {
			Transaction transaction = new Transaction();
			List<Map<String, Object>> array = JSON.parseObject(surroundCompanyList,
					new TypeReference<List<Map<String, Object>>>() {
					});
			List<String> scids = new ArrayList<>();
			String project_id = null;
			for (Map<String, Object> list : array) {
				System.out.println(list);
				EaoSurroundCompany surroundCompany = new EaoSurroundCompany();
				surroundCompany.setValue(list);
				project_id = surroundCompany.getProjectId();
				String id = surroundCompany.getScId();
				if (StringUtils.isEmpty(id)) {
					// 添加
					surroundCompany.setScId(UUID.randomUUID().toString());
					transaction.insert(surroundCompany);
					scids.add(surroundCompany.getScId());
				} else {
					// 更新
					scids.add(id);
					DataTable dt = new Q(" select * from eao_surround_company where ScId = ?", id).fetch();
					if (dt.getRowCount() != 0) {
						EaoSurroundCompany entity = new EaoSurroundCompany();
						entity.setValue(dt.get(0));
						entity.setValue(list);
						transaction.update(entity);
						transaction.commit();
					}

				}
			}
			DataTable dt = new Q(" select * from eao_surround_company where ProjectId = ?", project_id).fetch();
			if (dt.getRowCount() != 0) {
				for (DataRow dataRow : dt) {
					EaoSurroundCompany entity = new EaoSurroundCompany();
					entity.setValue(dataRow);
					Boolean del = true;
					for (String scid : scids) {
						if (entity.getScId().equals(scid)) {
							del = false;
						}
					}
					if (del) {
						transaction.delete(entity);
					}
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else {
			this.Response.put("data", "无操作记录");
		}
	}

	/**
	 * 根据Id删除一条记录
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String id = $V("ScId");
		if (StringUtils.isEmpty(id)) {
			f("操作失败，请确认操作信息");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_surround_company  where ").in("ScId", id);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

	/**
	 * 查询所有
	 */
	@Priv(loginType = LoginType.Member)
	public void findAll() {
		DataTable dt = new Q("select * from eao_surround_company ").fetch();
		List<EaoSurroundCompany> list = new ArrayList<>();
		for (DataRow dr : dt) {
			EaoSurroundCompany entity = new EaoSurroundCompany();
			entity.setValue(dr);
			list.add(entity);
			this.Response.put("data", list);
		}
	}

	/**
	 * 根据id查询单个
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String searchKey = $V("ScId");
		if (StringUtils.isEmpty(searchKey)) {
			f("请输入搜索条件！");
			return;
		}
		Q q = new Q("select * from eao_surround_company where ScId = ? ", searchKey);
		$S("data", q.fetch());
	}

	/**
	 * 根据项目ID查询其下的周围企业
	 */
	@Priv(loginType = LoginType.Member)
	public void findByProjectId() {
		String pid = $V("Id");
		if (StringUtils.isEmpty(pid)) {
			f("查询失败，请确认查询信息");
			return;
		}
		DataTable dt = new Q("select * from eao_surround_company where ProjectId = ? ", pid).fetch();
		List<EaoSurroundCompany> list = new ArrayList<>();
		for (DataRow dr : dt) {
			EaoSurroundCompany entity = new EaoSurroundCompany();
			entity.setValue(dr);
			list.add(entity);
			this.Response.put("data", list);
		}
	}
}
